<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>打开目录句柄</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.opendir" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">opendir</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">opendir</span> &mdash; <span class="dc-title">打开目录句柄</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.opendir-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>opendir</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$path</code></span>
   [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
  ] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   打开一个目录句柄，可用于之后的
   <span class="function"><a href="closedir.html" class="function">closedir()</a></span>，<span class="function"><a href="readdir.html" class="function">readdir()</a></span> 和
   <span class="function"><a href="rewinddir.html" class="function">rewinddir()</a></span> 调用中。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.opendir-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">path</code></dt>

     <dd>

      <p class="para">
       要打开的目录路径
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">context</code> 参数的说明见手册中的
       <a href="ref.stream.html" class="link">Streams API</a> 一章。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.opendir-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果成功则返回目录句柄的 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>，失败则返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   如果 <code class="parameter">path</code>
   不是一个合法的目录或者因为权限限制或文件系统错误而不能打开目录，<span class="function"><strong>opendir()</strong></span>
   返回 <strong><code>FALSE</code></strong> 并产生一个 <a href="errorfunc.constants.html" class="link">E_WARNING</a>
   级别的 PHP 错误信息。可以在 <span class="function"><strong>opendir()</strong></span>
   前面加上“<a href="language.operators.errorcontrol.html" class="link">@</a>”符号来抑制错误信息的输出。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.opendir-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.0</td>
       <td>
        <code class="parameter">path</code> 支持 <em>ftp://</em>
        URL wrapper
       </td>
      </tr>

      <tr>
       <td>4.3.0</td>
       <td>
        <code class="parameter">path</code> 可以是任何支持目录列表的
        URL，不过在 PHP 4 中只有 <em>file://</em>
        URL wrapper 支持此功能
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.opendir-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2751">
    <p><strong>Example #1 <span class="function"><strong>opendir()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dir&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/etc/php5/"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Open&nbsp;a&nbsp;known&nbsp;directory,&nbsp;and&nbsp;proceed&nbsp;to&nbsp;read&nbsp;its&nbsp;contents<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$dh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dh</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"filename:&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">&nbsp;:&nbsp;filetype:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">filetype</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: cgi : filetype: dir
filename: cli : filetype: dir
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.opendir-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="is_dir.html" class="function" rel="rdfs-seeAlso">is_dir()</a> - 判断给定文件名是否是一个目录</span></li>
    <li class="member"><span class="function"><a href="readdir.html" class="function" rel="rdfs-seeAlso">readdir()</a> - 从目录句柄中读取条目</span></li>
    <li class="member"><span class="function"><a href="dir.html" class="function" rel="rdfs-seeAlso">dir()</a> - 返回一个 Directory 类实例</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>